package com.hd.common.exception;

import com.hd.common.constant.SysConstant;
import com.hd.common.reqres.response.Result;
import com.hd.common.utils.ValueUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @Project 交通运输第三方监测平台
 * @Version: 1.0
 * @Author chentl
 * @Description: 异常处理器
 * @Pacakges:
 * @Create: 2020/12/2 0002 9:35
 */
@RestControllerAdvice
@Slf4j
public class SupExceptionHandler {

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(SupException.class)
    public Result<?> handleSupException(SupException e) {
        if (e != null) {
            log.error(e.getMsg(), e);
            return Result.error(ValueUtils.parseInteger(e.getCode()), e.getMessage());
        }
        return Result.error(SysConstant.Exception.UN_EXPECTED_ERROR);

    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public Result<?> handlerNoFoundException(Exception e) {
        log.error(e.getMessage(), e);
        return Result.error(SysConstant.NOT_FOUND, SysConstant.Exception.NOT_FOUND_MSG);
    }

    //扩展其他的异常捕获
    @ExceptionHandler(RuntimeException.class)
    public Result<?> normalException(Exception e) {
        log.error(e.getMessage(), e);
        return Result.error(SysConstant.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
